from threading import Thread, Lock
import time
valueNumber = 0
locks = Lock()

def worker(value):
    locks.acquire()
    global valueNumber
    for i in range(1000):
        valueNumber += 1
    print(valueNumber)
    locks.release()

def listen(value):
    locks.acquire()
    global valueNumber
    for i in range(1000):
        valueNumber -= 1
    print(valueNumber)
    locks.release()

if __name__ == '__main__':
    # 创建线程
    threadOne = Thread(target=worker, args=("不洛",))
    threadTwo = Thread(target=listen, args=("王五",))

    # 开启线程守护，如果主线程结束，子进程如果未结束，会被强制结束，如果不开启线程守护，主线程会等待子进程结束后结束
    # threadOne.daemon = True
    # threadTwo.daemon = True

    # 开启线程，子线程会执行被赋予的任务
    threadOne.start()
    threadTwo.start()
    # time.sleep(3)

    #线程的阻塞,等待子线程执行完,主线程才会继续执行
    threadOne.join()
    threadTwo.join()

    print("结束!")



    # test = threadValue("张三")
    # test.start()